﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;

namespace WebSite.cn
{
    public partial class Money : BasePage
    {
        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly DALCard theCard = new DALCard();
        readonly DALMember theMember = new DALMember();
        public string accountmoney = "0";
        tbMemberAccount oCardMember;

        string sTableBegin = "<table cellspacing=\"0\" cellpadding=\"4\" rules=\"all\" border=\"1\" style=\"border-color:#B80000;border-width:1px;border-style:Solid;width:100%;border-collapse:collapse;\">"
                       + " <tr style=\"background-color:#B80000;border-color:#B80000;border-width:3px;border-style:solid;height:25px;\">"
                      + " <td><font color='white'>序号</font></td><td><font color='white'>金额</font></td>"
                      + " <td><font color='white'>专区有效日期</font></td><td><font color='white'>商城有效日期</font></td>"
                      + " <td><font color='white'>已使用金頞<font></td><td><font color='white'>余额</font></td></tr>";
        string sTableFormat = " <tr style=\"height:25px\"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>";
        string sTableDetailFormat = "<tr  id=\"trDetail{0}\" style=\"display:none;\"> <td colspan=\"6\"  align=\"center\">{1}</td></tr>";              
        string sTableEnd = " </table>";

        string sSubTableBegin = "<table cellspacing=\"0\" cellpadding=\"4\" rules=\"all\" border=\"1\" style=\"border-color:#333333;border-width:1px;border-style:Solid;width:100%;border-collapse:collapse;\">"
                      + " <tr style=\"background-color:#333333;border-color:#333333;border-width:3px;border-style:solid;height:20px;\">"
                     + " <td><font color='white'>卡号</font></td><td><font color='white'>金额</font></td>"
                     + " <td><font color='white'>专区有效日期</font></td><td><font color='white'>商城有效日期</font></td>"
                     + " <td><font color='white'>已使用金頞<font></td><td><font color='white'>余额</font></td></tr>";
        string sSubTableFormat = " <tr style=\"height:20px\"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>";

        string sSubTableEnd = " </table>";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindCardList();
                this.GetAccountMoney();
            }
        }
        private void BindCardList()
        {
            DataSet ds;
            ds = theMemberAccount.GetGroupListByMemberId(LoginMember.MemberId);
            StringBuilder sbHtml = new StringBuilder();
            StringBuilder sbDetailHtml ;
            sbHtml.Append(sTableBegin);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach(DataRow dr in ds.Tables[0].Rows)
                {
                  sbHtml.Append(string.Format(sTableFormat
                                            , dr["number"].ToString() + "<a onclick=\"setDisplayState('trDetail" + dr["number"].ToString() + "')\">&nbsp;(明细)</a>"
                                            ,dr["cardPrice"].ToString()
                                            ,dr["endtime"].ToString()
                                            ,dr["lasttime"].ToString()
                                            ,dr["UsedMoney"].ToString()
                                            ,dr["remainMoney"].ToString()
                                           ));
                  DataSet dsDetail = theMemberAccount.GetListByMemberId(LoginMember.MemberId);

                  sbDetailHtml = new StringBuilder();
                  sbDetailHtml.Append(sSubTableBegin);
                  if (dsDetail != null && dsDetail.Tables.Count > 0)
                  {
                      foreach (DataRow drSub in dsDetail.Tables[0].Rows)
                      {
                          sbDetailHtml.Append(string.Format(sSubTableFormat
                                              , drSub["cardCode"].ToString()
                                              , drSub["cardPrice"].ToString()
                                              , drSub["endtime"].ToString()
                                              , drSub["lasttime"].ToString()
                                              , drSub["UsedMoney"].ToString()
                                              , drSub["remainMoney"].ToString()
                                             ));
                      }
                  }
                  sbDetailHtml.Append(sSubTableEnd);

                  sbHtml.Append(string.Format(sTableDetailFormat
                                             , dr["number"].ToString()
                                               , sbDetailHtml.ToString()
                                             ));
                    
                                                
                }
                sbHtml.Append(sTableEnd);
                this.divContent.InnerHtml = sbHtml.ToString();

                //this.GridView1.DataSource = ds.Tables[0].DefaultView;
                //this.GridView1.DataBind();
            }
        }

        private void GetAccountMoney()
        {

            if (base.LoginMember == null)
            {
                accountmoney = "0";
            }
            else
            {
                accountmoney = theMemberAccount.GetRemainSumMoney(base.LoginMember.MemberId).ToString();

            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (base.LoginMember == null)
            {
                base.Response.Write("<script language=javascript>window.location.href='index.aspx';</script>");
            }
            if (string.Compare(txtValidate.Text.Trim(), Session["checkCode"].ToString()) != 0)
            {
               // this.lblMessage.Text = "验证码不正确！请重新输入！";
                Response.Write("<script>alert('验证码不正确！请重新输入！');window.location.href='Money.aspx';</script>");
                return;
            }

            dbCard oCard;
            oCard = theCard.GetModel(this.txtCardCode.Text.Trim(), this.txtCardPassword.Text.Trim(),null);
            if (oCard != null)
            {
                if (oCard.states == 0)
                {
                    this.lblMessage.Text = "该卡还未激活，请先开卡后才能使用！";
                }
                else if (oCard.states == 2)
                {
                    this.lblMessage.Text = "该卡已被使用！";
                }
                else if (oCard.states == 3)
                {
                    this.lblMessage.Text = "该卡已失效！";
                }
                else if (oCard.states == 4)
                {
                    this.lblMessage.Text = "该卡还未激活,请先开卡后才能使用！";
                }
                else if (oCard.StartTime > DateTime.Now.Date)
                {
                    this.lblMessage.Text = "该卡还未生效！";
                }
                else if (oCard.lasttime < DateTime.Now.Date)
                {
                  
                        this.lblMessage.Text = "该卡已失效！";
                 
                }
                else if (DateTime.Now.Date > oCard.endtime && DateTime.Now.Date <= oCard.lasttime)
                {

                    if (oCard.states == 1)
                    {
                        theMemberAccount.ConvertToAccount(LoginMember.MemberId, oCard.cardid);
                        //   this.BindCardList();
                        Response.Write("<script>alert('充值成功！')</script>");
                        base.Response.Write("<script language=javascript>window.location.href='Money.aspx';</script>");
                        base.Response.End();

                    }
                    else
                    {
                        this.lblMessage.Text = "请联系客服！";
                    }


                }
                else if(DateTime.Now.Date <= oCard.endtime)
                {
                    this.lblMessage.Text = "该卡只能购买专区类商品！";
                }
            }
            else
            {
                this.lblMessage.Text = "卡号或密码不正确！";
            }
        }
    }
}